package com.lyun.controller;

import com.aliyuncs.exceptions.ClientException;
import com.lyun.bo.LoginReginfoBo;
import com.lyun.bo.LoginReginfoHeadBo;
import com.lyun.common.*;
import com.lyun.dto.LoginDto;
import com.lyun.dto.LoginReginfoDto;
import com.lyun.dto.LoginVerificationDto;
import com.lyun.pojo.Settings;
import com.lyun.pojo.User;
import com.lyun.properties.MsgProperties;
import com.lyun.service.SettingService;
import com.lyun.service.UserService;
import com.lyun.vo.LoginVerificationVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
//@CrossOrigin//跨域支持
public class UserController {

    @Resource
    private SendSms sendSms;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private UserService userService;

    @Resource
    private GetTags getTags;

    @Resource
    private OssTemplate ossTemplate;

    @Resource
    private SettingService settingService;

    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDto loginDto) throws ClientException {

        userService.login(loginDto);

        return R.success("验证码发送成功");
    }

    /**
     * 登录验证 并赋予token
     * -> 如果是新用户，需要给新用户默认的通用设置
     *
     * @param dto
     * @return
     * @throws ClientException
     */
    @PostMapping("/loginVerification")
    public LoginVerificationVo loginVerification(@RequestBody LoginVerificationDto dto) throws ClientException {

        return userService.loginVerification(dto);

    }

    @PostMapping("/loginReginfo")
    @Transactional(rollbackFor = Exception.class)
    public R<String> loginReginfo(@RequestHeader(name = "Authorization", required = false) String token,
                                  @RequestBody LoginReginfoDto dto) throws ParseException {

        //TODO 可以用threadLocal把下面重复的代码糅合
        Claims claims = JwtUtils.getClaims(token);
        String phone = claims.get("phone").toString();

        if (Objects.isNull(phone)) {
            return R.error("请求头参数异常");
        }

        Long id = BaseContext.getCurrentId();

        LoginReginfoBo bo = new LoginReginfoBo();
        BeanUtils.copyProperties(dto, bo);
        bo.setId(id);
        bo.setPhone(phone);

        return userService.loginReginfo(bo) ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 改头像
     *
     * @param headPhoto 头像文件
     */
    @PostMapping("/loginReginfo/head")
    public void loginReginfoHead(MultipartFile headPhoto) throws IOException {

        Long id = BaseContext.getCurrentId();
        LoginReginfoHeadBo bo = new LoginReginfoHeadBo();
        bo.setId(id);
        bo.setHeader(headPhoto);

        userService.loginReginfoHead(bo);

    }
}
